$(document).ready(function () {
    let token = getCookie('token');
    if (token === 'null' || token == null || token === '') {
        alert('登录失败')
        window.location.href = '../login.html'
    } else {
        alert('登录成功')
        console.log(token)
    }
})

function getCookie(name) {
    let cookies = document.cookie.split("; ")
    for (let i = 0; i < cookies.length; i++) {
        let arr = cookies[i].split("=")
        if (arr[0] === name) {
            return arr[1]
        }
    }
    return null
}

function fillTable(data) {
    let t_head = $("#myTable thead")
    let t_body = $("#myTable tbody")
    t_head.empty()
    t_body.empty()

    t_head.append('<tr>')
        .append('<th>学号</th>')
        .append('<th>姓名</th>')
        .append('<th>专业名称</th>')
        .append('<th>课程名称</th>')
        .append('<th>成绩</th>')
        .append('</tr>')

    $.each(data.data, function (index, item) {
        t_body.append('<tr>')
            .append('<td>' + item.studentId + '</td>')
            .append('<td>' + item.studentName + '</td>')
            .append('<td>' + item.majorName + '</td>')
            .append('<td>' + item.courseName + '</td>')
            .append('<td>' + item.score + '</td>')
            .append('</tr>')
    })
}


$('#b1').click(function () {
    $('#msg').text("（1）查询XXX同学各科成绩：学号，姓名，专业名称，课程名称，成绩")
    $.ajax({
        url: '/b1',
        type: 'get',
        headers: {'token': getCookie('token')},
        data: {
            text: $("#query").val()
        },
        dataType: 'json',
        success: function (data) {
            fillTable(data)
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})

$('#b2').click(function () {
    $('#msg').text("（2）按专业统计学生人数：专业，学生人数")
    $.ajax({
        url: '/b2',
        type: 'get',
        headers: {'token': getCookie('token')},
        dataType: 'json',
        success: function (data) {
            let t_head = $("#myTable thead")
            let t_body = $("#myTable tbody")
            t_head.empty()
            t_body.empty()

            t_head.append('<tr>')
                .append('<th>专业</th>')
                .append('<th>学生人数</th>')
                .append('</tr>')


            $.each(data.data, function (index, item) {
                t_body.append('<tr>')
                    .append('<td>' + item.majorName + '</td>')
                    .append('<td>' + item.number + '</td>')
                    .append('</tr>')
            })
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})

$('#b3').click(function () {
    $('#msg').text("（3）查询某门课程成绩单：学号，姓名，专业名称，成绩")
    $.ajax({
        url: '/b3',
        type: 'get',
        headers: {'token': getCookie('token')},
        data: {
            text: $("#query").val()
        },
        dataType: 'json',
        success: function (data) {
            fillTable(data)
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})

$('#b4').click(function () {
    $('#msg').text("（4）查询学生总学分，不及格不计学分：学号，姓名，专业名称，总学分")
    $.ajax({
        url: '/b4',
        type: 'get',
        headers: {'token': getCookie('token')},
        dataType: 'json',
        success: function (data) {
            let t_head = $("#myTable thead")
            let t_body = $("#myTable tbody")
            t_head.empty()
            t_body.empty()

            t_head.append('<tr>')
                .append('<th>学号</th>')
                .append('<th>姓名</th>')
                .append('<th>专业名称</th>')
                .append('<th>总学分</th>')
                .append('</tr>')


            $.each(data.data, function (index, item) {
                t_body.append('<tr>')
                    .append('<td>' + item.studentId + '</td>')
                    .append('<td>' + item.studentName + '</td>')
                    .append('<td>' + item.majorName + '</td>')
                    .append('<td>' + item.sumCredit + '</td>')
                    .append('</tr>')
            })
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})

$('#b5').click(function () {
    $('#msg').text("（5）查询不及格学生信息：学号，姓名，专业名称，补考课程名称")
    $.ajax({
        url: '/b5',
        type: 'get',
        headers: {'token': getCookie('token')},
        dataType: 'json',
        success: function (data) {
            let t_head = $("#myTable thead")
            let t_body = $("#myTable tbody")
            t_head.empty()
            t_body.empty()

            t_head.append('<tr>')
                .append('<th>学号</th>')
                .append('<th>姓名</th>')
                .append('<th>专业名称</th>')
                .append('<th>补考课程名称</th>')
                .append('</tr>')


            $.each(data.data, function (index, item) {
                t_body.append('<tr>')
                    .append('<td>' + item.studentId + '</td>')
                    .append('<td>' + item.studentName + '</td>')
                    .append('<td>' + item.majorName + '</td>')
                    .append('<td>' + item.courseName + '</td>')
                    .append('</tr>')
            })
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})

$('#b6').click(function () {
    $('#msg').text("（6）查询奖学金学生信息（要求：无不及格记录，且平均分在85分以上）：学号，姓名，专业名称，平均分")
    $.ajax({
        url: '/b6',
        type: 'get',
        headers: {'token': getCookie('token')},
        dataType: 'json',
        success: function (data) {
            let t_head = $("#myTable thead")
            let t_body = $("#myTable tbody")
            t_head.empty()
            t_body.empty()

            t_head.append('<tr>')
                .append('<th>学号</th>')
                .append('<th>姓名</th>')
                .append('<th>专业名称</th>')
                .append('<th>平均分</th>')
                .append('</tr>')


            $.each(data.data, function (index, item) {
                t_body.append('<tr>')
                    .append('<td>' + item.studentId + '</td>')
                    .append('<td>' + item.studentName + '</td>')
                    .append('<td>' + item.majorName + '</td>')
                    .append('<td>' + item.avgScore + '</td>')
                    .append('</tr>')
            })
        },
        error: function () {
            console.log('Error occurred while fetching JSON data.')
        }
    })
})